package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class PTransactionsSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(PTransactionsSearchResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");	
		TransferRgActionForm inForm = (TransferRgActionForm) form;
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		Codes code = Codes.getInstance();
		if (inForm.getSerial() == null || "".equals(inForm.getSerial())) {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}
		
		TransferAcceptedInfoDto dto = TransferAcceptedInfo.getInstance().getAssessInfo001(inForm.getSerial());
        if(dto.getTransferRegister() ==2) {
        	setErrorsToRequest(request, TmsConstants.U1060, null);
			return mapping.getInputForward();
		}else if(dto.getTransferRegister() ==3) {
			setErrorsToRequest(request, TmsConstants.U1061, null);
			return mapping.getInputForward();
		}else if(dto.getTransfer() ==2) {
			setErrorsToRequest(request, TmsConstants.U1062, null);
			return mapping.getInputForward();
		}else if(dto.getCy_printTime() == null) {
			setErrorsToRequest(request, TmsConstants.U1063, null);
			return mapping.getInputForward();
		}else if(dto.getGh_printTime() != null && dto.getChangeTradeTicket()==2) {
			setErrorsToRequest(request, TmsConstants.IM021, null);
			return mapping.getInputForward();
		}
		 
		uc.setAttribute(TmsConstants.VIEWHELP, toViewHelper(dto,view));
		logger.debug(this.getClass().getName() + "#execute was completed.");	
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	
	
	public static TransferRgInputViewHelper toViewHelper(TransferAcceptedInfoDto dto,TransferRgInputViewHelper view) throws TmsDaoException {
		if(dto.getOwnerType() == 1) view.setIdentification(dto.getCarOwnerIdCardNo());
		else view.setIdentification(dto.getOwnerOrgNo());
		
		if(dto.getNewOwnerType() == 1) view.setNewIdentification(dto.getNewcarOwnerIdCardNo());
		else view.setNewIdentification(dto.getNewOwnerOrgNo());
		
		Codes code = Codes.getInstance();
		if(StringUtil.isNotBlank(dto.getCarType())) view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,Integer.parseInt(dto.getCarType())));
		view.setCarType(dto.getCarType());
		if(StringUtil.isNotBlank(dto.getCarUse())) view.setCarUseName(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(dto.getCarUse())));
		view.setCarUse(dto.getCarUse());
		view.setMarketName(dto.getMarketName());
		view.setTransMarket(dto.getTransMarket());
		view.setVaoName(dto.getVaoName());
		view.setCarMarketIn(dto.getCarMarketInCity()+"");
		view.setTransNo(dto.getTransNo());
		view.setCheckUserName(dto.getRealName());
		view.setCheckUserId(dto.getCheckUserid()+"");
		view.setSerial(dto.getSerial());
		view.setLicensePlate(dto.getLicensePlate());
		view.setVin(dto.getVin());
		view.setCarType(dto.getCarType());
		view.setRegisterCardNo(dto.getRegisterCardNo());
		view.setPrice(dto.getPrice() +"");
		view.setVendorCode(dto.getVendorCode());
		view.setModelCode(dto.getModelCode());
		view.setRealName(dto.getRealName());
		view.setOwnerType(dto.getOwnerType() +"");
		view.setCarOwnerIdCardNo(dto.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(dto.getOwnerOrgNo());
		view.setCarOwner(dto.getCarOwner());
		view.setOwnerPhone(dto.getOwnerPhone());
		view.setOwnerAddress(dto.getOwnerAddress());
		view.setNewOwnerType(dto.getNewOwnerType() +"");
		view.setNewcarOwnerIdCardNo(dto.getNewcarOwnerIdCardNo());
		view.setNewOwnerOrgNo(dto.getNewOwnerOrgNo());
		view.setNewCarOwner(dto.getNewcarOwner());
		view.setNewOwnerPhone(dto.getNewOwnerPhone());
		view.setNewOwnerAddress(dto.getNewOwnerAddress());
		view.setChangeTradeTicket(dto.getChangeTradeTicket());
		view.setPg_printNum(dto.getPg_printNum() +"");
		view.setPg_printUserId(dto.getPg_printUserId() +"");
		view.setOther(dto.getOther() +"");
		view.setMonitoringSales(dto.getMonitoringSales() +"");
		view.setChangeBody(dto.getChangeBody()+"");
		view.setChangeTradeTicket(dto.getChangeTradeTicket());
		view.setCardType(dto.getCardType()+"");
		return view;
	}
	
}
